package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.exam.IPKMorePersonConfirmView;
import com.yaozheng.vocationaltraining.iview.exam.IPKMorePersonHallView;
import com.yaozheng.vocationaltraining.service.exam.PKMorePersonConfirmService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * Created by moon on 2017/11/16.
 */

@EBean
public class PKMorePersonConfirmServiceImpl implements PKMorePersonConfirmService {
    IPKMorePersonConfirmView iTokenBaseView;
    @Override
    public void init(IPKMorePersonConfirmView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Background
    @Override
    public void list(int roomId,final boolean flag) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/multi-player/room/"+roomId,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.responseListSuccess(jsonObject,flag);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.responseError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void start(int roomId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/multi-player/game/"+roomId+"/join",
                    iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.startPkMorePersonSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.startPkMorePersonError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .startPkMorePersonError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void leave(int gameId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/multi-player/game/"+gameId+"/leave",
                    iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.leaveSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.leaveError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .leaveError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void loadQuestionList(int gameId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/withPlayers/"+gameId, iTokenBaseView.getToken(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.getQuestionListSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.getQuestionListError(error);

                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .getQuestionListError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }


}
